package com.cqut.forum.dao.imp;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.cqut.forum.dao.PictureGroupDao;
import com.cqut.forum.entity.PictureGroup;

/**
 * 图片分组dao实现类
 * @author yangxun
 */
@Component
public class PictureGroupDaoImp extends BaseDaoImp implements PictureGroupDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<PictureGroup> getGroups(int userId) {
		String hql = "select pg from PictureGroup pg where pg.ownerId = ?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(userId);
		
		List<PictureGroup> result = (List<PictureGroup>) super.excuteHql(hql, parameters, null);
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PictureGroup getByName(int userId,String pgName) {
		String hql = "select pg from PictureGroup pg where pg.pictureGroupName = '"+pgName+"' " +
				"and pg.ownerId="+userId;
		List<PictureGroup> result = super.getHibernateTemplate().find(hql);
		return result.size()>0?result.get(0):null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getGroupId(int pictureId) {
		String sql = "select pg.picture_group_id from picture p,picture_group pg " +
				"where p.picture_id="+pictureId+" and p.picture_group_id=pg.picture_group_id";
		List<Map<String,Object>> result = (List<Map<String,Object>>)super.excuteSql(sql,null,null);
		return (Integer)result.get(0).get("picture_group_id");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getGroupsLikeName(String groupName) {
		String sql = "select pg.picture_group_id from picture_group pg where pg.is_public=true and " +
				"pg.picture_group_name like \""+groupName+"\"";
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql,null, null);
		
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getSysteGroups() {
		String sql = "select u.user_name,pg.is_public,pg.picture_group_id,pg.picture_group_name,pg.picture_number from picture_group pg,user u " +
				"where pg.owner_id=u.user_id and pg.owner_id in " +
				"(" +
				"select u.user_id from user u,role r where u.role_id=r.role_id and (r.role_name=\"管理员\" or r.role_name=\"站长\")" +
				")";
		
		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql,null, null);
		
		return result;
	}

	@Override
	public boolean update(int groupId, String groupName,boolean state) {
		String sql = "update picture_group pg set pg.picture_group_name=?,pg.is_public=? where pg.picture_group_id=?";
		
		Query query = super.getSqlQuery(sql);
		query.setString(0, groupName);
		query.setBoolean(1, state);
		query.setInteger(2, groupId);
		query.executeUpdate();
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PictureGroup getByName(String pgName) {
		String hql = "select pg from PictureGroup pg where pg.pictureGroupName = ?";
		List<Object> parameters = new ArrayList<Object>();
		parameters.add(pgName);
		
		List<PictureGroup> result = (List<PictureGroup>) super.excuteHql(hql, parameters, null);
		
		return result.size()>0?result.get(0):null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getShowGroups() {
		String sql = "select u.user_name,pg.is_public,pg.picture_group_id,pg.picture_group_name,pg.picture_number from picture_group pg,user u " +
		"where pg.owner_id=u.user_id and pg.is_public=true and pg.owner_id in " +
		"(" +
		"select u.user_id from user u,role r where u.role_id=r.role_id and (r.role_name=\"管理员\" or r.role_name=\"站长\")" +
		")";

		List<Map<String, Object>> result = (List<Map<String, Object>>) super.excuteSql(sql,null, null);

		return result;
	}

}
